package com.laiat.yamba.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.laiat.yamba.Manifest;
import com.laiat.yamba.R;
import com.laiat.yamba.TwitterResultReceiver;
import com.laiat.yamba.UpdaterService;
import com.laiat.yamba.YambaApplication;

public class BaseActivity extends Activity {

	protected YambaApplication yamba;
	private Menu optionsMenu;
	
	private TwitterResultReceiver twitterResultReceiver;
	
	public static final int DIALOG_TWITTER_LOGIN = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		yamba = ((YambaApplication) getApplication());
		
		twitterResultReceiver = new TwitterResultReceiver(this);
	}

	// Called first time user clicks on the menu button
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		optionsMenu = menu;
    	MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
    	
    	return true;
	}
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menuItemPrefs:
				startActivity(
						new Intent(this, PrefsActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
			break;
			
			case R.id.menuItemStatus:
				startActivity(new Intent(this, StatusActivity.class)
									.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
									.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
			break;
			
			case R.id.menuItemTimeline:
				startActivity(
						new Intent(this, TimelineActivity.class)
							.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
							.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
			break;
			
			case R.id.menuItemTimelinePurge:
				//Delete the statuses from timeline
				yamba.getStatusData().delete();
				
				Toast.makeText(this, R.string.msgAllDataPurged, Toast.LENGTH_LONG).show();
			break;
			
			case R.id.menuItemToggleTwitterCredentials:
				if (yamba.hasTwitterCredentials()) {
					yamba.removeTwitterCredentials();
				} else {
					yamba.loginToTwitter();
				}
			break;
						
			case R.id.menuItemExit:
				//FIXME:  call all activities in the app not just this one activity
				finish();
			break;
			
			case R.id.menuItemRefreshUpdateService:
				startService(new Intent(this, UpdaterService.class));
			break;
		}
		
		return true;
	}
	
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		MenuItem toggleTwitterMenuItem = menu.findItem(R.id.menuItemToggleTwitterCredentials);
		if (yamba.hasTwitterCredentials()) {
			toggleTwitterMenuItem.setTitle(R.string.titleTwitterSignout);
			toggleTwitterMenuItem.setIcon(android.R.drawable.ic_input_delete);
		} else {
			toggleTwitterMenuItem.setTitle(R.string.titleTwitterLogin);
			toggleTwitterMenuItem.setIcon(android.R.drawable.ic_input_get);
		}

	
		return true;
	}

	public Menu getOptionsMenu() {
		return optionsMenu;
	}
	
	//For 2.1
	@Override
	protected Dialog onCreateDialog(int id) {
		return onCreateDialog(id, null); 
	}

	//Comment out for 2.1
//	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		
		Dialog dialog = null;
	    switch(id) {
		    case DIALOG_TWITTER_LOGIN:
	    		Log.d(this.getClass().getSimpleName(), "onCreateDialog for Twitter Login");
	    		dialog = createTwitterLoginRequestDialog();
		        break;
		    default:
		        dialog = null;
	    }
	    return dialog;
	}

	public AlertDialog createTwitterLoginRequestDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("No twitter account is set.  Do you want to login to Twitter?")
		       .setCancelable(false)
		       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   dialog.dismiss();
		        	   yamba.loginToTwitter();
		           }
		       })
		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   dialog.cancel();
		           }
		       });
		return builder.create();
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		registerReceiver(twitterResultReceiver, 
				 new IntentFilter(YambaApplication.BROADCAST_TWITTER_RESULT),
				 Manifest.permission.SEND_TWITTER_RESULT,
				 null);
		
	}

	@Override
	protected void onPause() {
		super.onPause();
		
		unregisterReceiver(twitterResultReceiver);
	}

	
	
}
